package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.mapper.EduCommentMapper;
import com.atguigu.eduservice.service.EduCommentService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-10-22
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {


    //1.分页根据课程id查询课程评论的方法
    @Override
    public Map<String, Object> getCourseRemark(Page<EduComment> pageParam,String courseId) {
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(pageParam, wrapper);

        List<EduComment> records = pageParam.getRecords();   //每页数据list集合
        long current = pageParam.getCurrent();              //当前页
        long pages = pageParam.getPages();                  //总页数
        long size = pageParam.getSize();                    //每页记录数
        long total = pageParam.getTotal();                  //总记录数
        boolean hasNext = pageParam.hasNext();              //是否有下一页
        boolean hasPrevious = pageParam.hasPrevious();      //是否有上一页

        //把分页数据获取出来，放到map集合
        Map<String,Object> map=new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        //map返回
        return map;
    }

    //2.添加评论的方法
    @Override
    public void saveComment(EduComment eduComment) {
        //获取评论的数据
        String content = eduComment.getContent();
        String memberId = eduComment.getMemberId();
        String avatar = eduComment.getAvatar();
        String courseId = eduComment.getCourseId();
        String teacherId = eduComment.getTeacherId();
        String nickname = eduComment.getNickname();

        EduComment eduComment1=new EduComment();
        eduComment1.setMemberId(memberId);
        eduComment1.setAvatar(avatar);
        eduComment1.setCourseId(courseId);
        eduComment1.setContent(content);
        eduComment1.setTeacherId(teacherId);
        eduComment1.setNickname(nickname);

        //添加进数据库
        baseMapper.insert(eduComment1);

    }
}
